package com.mcxx.modules.miniSalvation.order.service.write;

import com.mcxx.common.exception.BizException;
import com.mcxx.convert.OrderConvert;
import com.mcxx.modules.miniSalvation.order.dao.write.OrderCertWriteDao;
import com.mcxx.modules.miniSalvation.order.dao.write.OrderInHouseWriteDao;
import com.mcxx.modules.miniSalvation.order.dto.OrderInHouseDTO;
import com.mcxx.modules.miniSalvation.family.entity.OrderInHouseEntity;
import com.mcxx.modules.temporary.order.dao.write.TmpOrderCertWriteDao;
import com.mcxx.util.Constant;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@TaTransactional
public class InHouseWriteService extends BaseService {
    @Autowired
    private OrderInHouseWriteDao inHouseWriteDao;
    @Autowired
    private OrderCertWriteDao orderCertWriteDao;
    @Autowired
    private TmpOrderCertWriteDao tmpOrderCertWriteDao;



    public int addInHouse(OrderInHouseDTO orderInHouse, List<String> orderCertIdList, UserAccountVo userAccountVo) {


        log.info("保存入户调查信息");
        orderInHouse.setId(Constant.getUUID());
        orderInHouse.setState(Constant.InHouseState.WAIT_CHECK);
        OrderInHouseEntity orderInHouseEntity = OrderConvert.convertOrderInHouseEntity(orderInHouse);

        orderInHouseEntity.setUserId(userAccountVo.getUserId());
        orderInHouseEntity.setUserAreaLevel(userAccountVo.getAreaLevel());

        int rst = inHouseWriteDao.insert(orderInHouseEntity);
        if(rst <= 0){
            log.info("保存入户调查信息，无法插入更新数据");
            throw new BizException("-1", "保存入户调查信息，无法插入更新数据");
        }

        if(null == orderCertIdList || orderCertIdList.size() <= 0){
            return 1;
        }
        log.info("更新对应的证明材料关系,材料：{}", orderCertIdList);
        if(Constant.BusinessCode.LINSHI.equals(orderInHouse.getCheckResult())){
            rst = tmpOrderCertWriteDao.updateById(orderCertIdList, orderInHouse.getOrderId(), "3", orderInHouse.getId());
        }else{
            rst = orderCertWriteDao.updateById(orderCertIdList, orderInHouse.getOrderId(), "3", orderInHouse.getId());
        }

        if(rst <= 0){
            log.info("保存入户调查信息-更新证明材料关系 异常，无法插入更新数据");
            throw new BizException("-1", "保存入户调查信息的证明材料失败");
        }
        return 1;
    }


    public  void updateById(OrderInHouseDTO orderInHouse, List<String> orderCertIdList, UserAccountVo userAccountVo) {

        log.info("保存入户调查信息");
        OrderInHouseEntity orderInHouseEntity = OrderConvert.convertOrderInHouseEntity(orderInHouse);

        orderInHouseEntity.setUserId(userAccountVo.getUserId());
        orderInHouseEntity.setUserAreaLevel(userAccountVo.getAreaLevel());

        int rst = inHouseWriteDao.updateById(orderInHouseEntity);
        if(rst <= 0){
            throw new BizException("-1", "保存入户抽查信息失败");

        }

        if(null == orderCertIdList || orderCertIdList.size() <= 0){
            return;
        }
        log.info("更新对应的证明材料关系,材料：{}", orderCertIdList);
        if(Constant.BusinessCode.LINSHI.equals(orderInHouse.getCheckResult())){

            rst = tmpOrderCertWriteDao.updateById(orderCertIdList, orderInHouse.getOrderId(), "3", orderInHouse.getId());
        }else{
            rst = orderCertWriteDao.updateById(orderCertIdList, orderInHouse.getOrderId(), "3", orderInHouse.getId());
        }
        if(rst <= 0){
            log.info("保存入户调查信息-更新证明材料关系 异常，无法插入更新数据");
            throw new BizException("-1", "保存入户调查信息的证明材料失败");
        }
    }


    public int addTempInHouse(OrderInHouseDTO orderInHouse, List<String> orderCertIdList, UserAccountVo userAccountVo) {


        log.info("保存入户调查信息");
        orderInHouse.setId(Constant.getUUID());
        orderInHouse.setState(Constant.InHouseState.WAIT_CHECK);
        OrderInHouseEntity orderInHouseEntity = OrderConvert.convertOrderInHouseEntity(orderInHouse);

        orderInHouseEntity.setUserId(userAccountVo.getUserId());
        orderInHouseEntity.setUserAreaLevel(userAccountVo.getAreaLevel());

        int rst = inHouseWriteDao.insert(orderInHouseEntity);
        if(rst <= 0){
            log.info("保存入户调查信息，无法插入更新数据");
            throw new BizException("-1", "保存入户调查信息，无法插入更新数据");
        }

        if(null == orderCertIdList || orderCertIdList.size() <= 0){
            return 1;
        }
        log.info("更新对应的证明材料关系,材料：{}", orderCertIdList);
        rst = tmpOrderCertWriteDao.updateById(orderCertIdList, orderInHouse.getOrderId(), "3", orderInHouse.getId());


        if(rst <= 0){
            log.info("保存入户调查信息-更新证明材料关系 异常，无法插入更新数据");
            throw new BizException("-1", "保存入户调查信息的证明材料失败");
        }
        return 1;
    }


    public  void updateTempById(OrderInHouseDTO orderInHouse, List<String> orderCertIdList, UserAccountVo userAccountVo) {

        log.info("保存入户调查信息");
        OrderInHouseEntity orderInHouseEntity = OrderConvert.convertOrderInHouseEntity(orderInHouse);

        orderInHouseEntity.setUserId(userAccountVo.getUserId());
        orderInHouseEntity.setUserAreaLevel(userAccountVo.getAreaLevel());

        int rst = inHouseWriteDao.updateById(orderInHouseEntity);
        if(rst <= 0){
            throw new BizException("-1", "保存入户抽查信息失败");

        }

        if(null == orderCertIdList || orderCertIdList.size() <= 0){
            return;
        }
        log.info("更新对应的证明材料关系,材料：{}", orderCertIdList);
        rst = tmpOrderCertWriteDao.updateById(orderCertIdList, orderInHouse.getOrderId(), "3", orderInHouse.getId());

        if(rst <= 0){
            log.info("保存入户调查信息-更新证明材料关系 异常，无法插入更新数据");
            throw new BizException("-1", "保存入户调查信息的证明材料失败");
        }
    }



    public void deleteByOrderId(String orderId){

        orderCertWriteDao.deleteByOrderAndAssociationTable(orderId, "3");

        inHouseWriteDao.deleteByOrderId(orderId);
    }


    public int copyInHouseInfo(String oldId,String newId,String newOrderId){
        return inHouseWriteDao.copyInHouseInfo(oldId,newId,newOrderId);
    }

}
